<?php
namespace app\adm\controller;

use think\Db;

class Usergroup extends Base
{
	// 代理列表
	public function index() 
	{
		if (is_post()) {
			if (input('get.act') == 'rm') {
				$rst = db('ns_user')->where('group_id',input('post.id'))->count();
				if($rst)
					return $this->error('请先移除该等级下的'.$rst.'个用户');
				return model('usergroup')->rmusergroup(input('post.id'));
			}
			if (input('get.act') == 'get') {
				return model('usergroup')->getInfo(input('post.id'));
			}
		}

		$get = [];
		// if (is_get()) {
		// 	$get = input('get.');
		// 	$get['status'] = 1;
		// }


		$list = model('usergroup')->getList($get);

		// $this->assign('province',db('cn_area')->where('is_p', 1)->select());
		$this->assign('list', $list);
		return $this->fetch();
	}

	// 添加，修改代理信息
	public function addGroup()
	{
		if (is_post()) {

			$post = input('post.');

			if (!($post['group_name'] && is_numeric($post['quota']) && is_numeric($post['spread']) && is_numeric($post['take_per']) && is_numeric($post['sign_reward']) && is_numeric($post['sign_condition']))) {
				$this->error('请完善信息！');
			}

			if ($post['id']) {
				return model('usergroup')->editInfo($post);
			}
		
			return model('usergroup')->addInfo($post);
		}
	}

	// 获取代理信息
	public function getInfo()
	{
		$uid = input('get.id');
		$info = db('member')->where('uid',$uid)->find();
		return $info;
	} 

	// 获取省列表
	public function getProvince()
	{
		$list = db('cn_area')->where('is_p', 1)->select();
		return $list;
	}

	// 获取城市
    public function getCity()
    {
        $pro = input('get.p_code');
        $list = db('cn_area')->where(['p_code' => $pro, 'is_c' => 1])->select();
        return ['city'=>$list];
    }

    // 获取区域
    public function getDist()
    {
        $c_code = input('get.c_code');
        $list = db('cn_area')->where(['c_code' => $c_code, 'is_c' => 0])->select();
        return ['dist'=>$list];
    }

}